package org.example.poiwebtest;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import org.apache.commons.lang3.StringUtils;
import org.example.poiwebtest.entity.PhoneDTO;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;


@Component
public class PhoneExcelverifiyName implements IExcelVerifyHandler<PhoneDTO> {

    //保存表格中的手机名称
    private static Set<String> stringSet = new HashSet<>();

    @Override
    public ExcelVerifyHandlerResult verifyHandler(PhoneDTO phoneDTO) {

        //设置默认验证为true
        ExcelVerifyHandlerResult excelVerifyHandlerResult = new ExcelVerifyHandlerResult(true);
        if (StringUtils.isNotBlank(phoneDTO.getPhoneName())) {
            stringSet.add(phoneDTO.getPhoneName());
            StringBuilder str = new StringBuilder();

            //校验表格中手机名称是否一致, 不一致,给出错误提示
            if (stringSet.size()>1){
                excelVerifyHandlerResult.setSuccess(false);
                str.append("手机名称不一致 ");
            }

            // 手机名为小米 则为重复录入
            if ("小米".equals(phoneDTO.getPhoneName())) {

                if (excelVerifyHandlerResult.isSuccess()){
                    excelVerifyHandlerResult.setSuccess(false);
                    str.append("手机名称重复");
                }else {
                    str.append(",手机名称重复");
                }
            }

            excelVerifyHandlerResult.setMsg(str.toString());

        }
        return excelVerifyHandlerResult;
    }
}

